-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cumulative sum tensor operation #1722
base: main
Are you sure you want to change the base?
Conversation
Hi @allenqm
I'm willing to write that kernel in the JIT intermediate representation if you want, so the operation becomes available soon; then we can optimize it later and with the upcoming language. |
@louisfd Thanks so much for the guidance. I will remove _dim suffix. Thanks for offering to step in and write the kernel in the JIT intermediate representation. I'll take you up on that. I'm going to try and get the tch, candle, ndarray, and autodiff implementations done by EoD tomorrow. Just to be clear: I haven't written anything specific for cumprod yet. I was proposing that if we implement cumsum, then cumprod will be more straightforward as it could be described without new backend implementations (with the exception of autodiff), using the existing implementations of cumsum, exp, and log. Let me know if my assessment here seems off. |
tensor: NdArrayTensor<E, D>, | ||
dim: usize, | ||
) -> NdArrayTensor<E, D> { | ||
let mut array = tensor.array.clone().into_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the underlying array struct of tensor
needs to be cloned, since NdArray's method for accumulating elements along an axis modifies an array's data inplace. Referring to this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well float_cumsum
takes ownership of the tensor
, so I don't think the clone is required here.
tch, candle, ndarray, autodiff + tests, and tensor tests have been added. Going to work on the onnx section of the contributor book next. no action needed, just fyi @louisfd |
This PR has been marked as stale because it has not been updated for over a month |
Sorry for not flipping this to "Ready for Review" @louisfd . I think I've got the required onnx files in place. Can you take a look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some minor comments, but overall great job 👏
FloatCumsum, | ||
IntCumsum, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CumSum
is a single operator, the same for int and float.
So we should also only have one node type, BinaryType::Cumsum
. See for example BinaryType::Sub
, which does split the int and float tests for the generated onnx files but it's still a single operation/node.
@@ -770,6 +770,18 @@ pub trait IntTensorOps<B: Backend> { | |||
/// The sum of all elements in the tensor along the dimension. | |||
fn int_sum_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>; | |||
|
|||
/// Cumulative Sum of all elements in a tensor along a dimension. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the capitalization at "Cumulative sum"
@@ -842,6 +842,19 @@ pub trait FloatTensorOps<B: Backend> { | |||
/// A tensor with the sum of all elements in `tensor` along `dim`. | |||
fn float_sum_dim<const D: usize>(tensor: FloatTensor<B, D>, dim: usize) -> FloatTensor<B, D>; | |||
|
|||
/// Cumulative Sum of all elements in a tensor along a dimension. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing regarding capitalization
/// Checks running dimension such as cumulative sum | ||
pub(crate) fn running_dim<const D: usize>(ops: &str, dim: usize) -> Self { | ||
let mut check = Self::Ok; | ||
|
||
if dim > D { | ||
check = check.register( | ||
ops, | ||
TensorError::new(format!( | ||
"Can't perform a running calculation on a tensor with ({D}) dimensions on axis ({dim})" | ||
)), | ||
); | ||
} | ||
|
||
check | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the existing TensorCheck::dim_ops
instead
match &lhs { | ||
Type::Tensor(x) => match x.kind { | ||
TensorKind::Int => BinaryNode::int_cumsum(lhs, rhs, output), | ||
TensorKind::Float => BinaryNode::float_cumsum(lhs, rhs, output), | ||
_ => panic!("cumsum function requires LHS to be int or float type"), | ||
}, | ||
_ => panic!("cumsum function only supports LHS tensor type"), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By making cumsum a single node it should simplify this block
tensor: NdArrayTensor<E, D>, | ||
dim: usize, | ||
) -> NdArrayTensor<E, D> { | ||
let mut array = tensor.array.clone().into_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well float_cumsum
takes ownership of the tensor
, so I don't think the clone is required here.
tensor: NdArrayTensor<i64, D>, | ||
dim: usize, | ||
) -> NdArrayTensor<i64, D> { | ||
let mut array = tensor.array.clone().into_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment for float_cumsum
This PR has been marked as stale because it has not been updated for over a month |
Labeled this PR as needs help completing it. It would be a great feature to have in Burn. |
Sorry for the delay. I am planning to devote time this Friday to make the
requested changes.
…On Sun, Aug 4, 2024 at 3:34 PM Dilshod Tadjibaev ***@***.***> wrote:
Labeled this PR as needs help completing it. It would be a great feature
to have in Burn.
—
Reply to this email directly, view it on GitHub
<#1722 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABCYGJFVI5ON7SG3X23M4FLZPZ62TAVCNFSM6AAAAABHGF7VYWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRXGY2DKNRYG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This PR has been marked as stale because it has not been updated for over a month |
Pull Request Template
Starting a draft PR to align on a few things with maintainers as I dive into this.
Context: Per this convo, I wanted to add a cumulative product operation to burn.
My plan is to start with a cumulative sum operation. Then cumulative product can be developed using cumulative sum, log, and exp.
@nathanielsimard, Items to align on upfront:
cumsum_dim
.cumsum
aligns with the pytorch api. In burn, operations that take an explicit dim argument seem to have a_dim
suffix. Alternatively we could remove the suffix.Checklist
run-checks all
script has been executed.Related Issues/PRs
Provide links to relevant issues and dependent PRs.
Changes
Summarize the problem being addressed and your solution.
Testing
Describe how these changes have been tested.